const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const path = require('path');
const { merge } = require('webpack-merge');

const globalConfig = require('./config');
const webpackCommon = require('./webpack.common');
const vueConfig = require('./feature/vue.config');
const reactConfig = require('./feature/react.config');

module.exports = function(calcConfig, alias = false){
    const moduleConfig = require(path.join(calcConfig.modulePath, 'config.js'));
    const type = moduleConfig.type;
    
    const configArray = [ webpackCommon(calcConfig, moduleConfig) ];
    configArray.push( type === 'vue' ? vueConfig(calcConfig, alias) : reactConfig(calcConfig) );
    const moduleName = calcConfig.moduleName;

    configArray.push({
        mode: 'production',
        // mode: 'development',
        output: {
            path: path.join(calcConfig.basePath, globalConfig.output, alias ? globalConfig.prefix.moduleFix + moduleName: moduleName),
			filename: `${globalConfig.prefix.js}/[name]_[hash:8].js`,
            chunkFilename: `${globalConfig.prefix.js}/[name]_[chunkhash:8].js`,
            // publicPath: '../'
            publicPath: `https://app.hxsapp.com/hxswap/${calcConfig.moduleName}/`
        },
        plugins: [
            new MiniCssExtractPlugin({
                filename: `${globalConfig.prefix.css}/[name].[hash].css`,
                chunkFilename: `${globalConfig.prefix.css}/[name].[hash].css`,
            }),
            new OptimizeCSSAssetsPlugin()
        ]
    });

    return merge(...configArray);
}